
import { BaseController } from "../../cfw/mvc/BaseController";
import { IControllerTarget } from "../../cfw/mvc/IControllerTarget";
import { engine } from "../../engine/engine";
import { BehaviorTree } from "./BehaviorTree";
import { BehaviorTreeFactory } from "./BehaviorTreeFactory";


export class BehaviorTreeController<T extends IControllerTarget = any> extends BaseController<T>{
    protected tree: BehaviorTree;
    protected factory: BehaviorTreeFactory;
    constructor(path: string) {
        super()
        this.factory = new BehaviorTreeFactory(path)
    }
    protected isThinking: boolean = false;

    check(dt: number): boolean {
        return false;
    }
    setTarget(t: T): void {
        this.target = t;
    }

    think(state: number) {
        if (this.tree) {
            this.tree.reset()
            this.isThinking = true;
            // Debug.log('BehaviorTreeController think ')
            this.tree.setEndCallback(() => {
                this.isThinking = false;
                // Debug.log('BehaviorTreeController think finish')
            })
        }
    }



    setTree(jsonName: string, bundleName: string) {
        if (!this.target) {
            engine.warn(' target is null ')
            return
        }
        this.tree = this.factory.buildTree(jsonName, bundleName, this.target)
    }

    updateLogic(dt: number): void {
        if (!this.isThinking) {
            return;
        }
        if (this.tree) {
            this.tree.tick()
        }
    }
}